/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.data.multilang;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.iec.edp.caf.data.CAFCurrentLanguageResolver;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.Nationalized;

import javax.persistence.Embeddable;
import java.util.*;

/**
 * 多语嵌入类型映射列
 * 项目扩展时暂时可以重写，扩展该类。
 * 增加语言属性，扩展getValue方法，在MclColumns集合增加语言属性列名
 */
@Setter
@Getter
@Slf4j
@Embeddable
public class CAFMultiLanguageColumn {

    /**
     * 语言属性列名集合
     */
    public static final List<String> MlcColumn = new ArrayList<String>() {{
        add("cafMlcchs");
        add("cafMlcen");
        add("cafMlccht");
        add("cafMlcpt");
        add("cafMlces");
    }};

    public static final String fieldPrefix = "cafMlc";

    /**
     * 语言属性的命名规则：cafMlc+语言后缀
     */
    private String cafMlcchs;
    private String cafMlcen;
    private String cafMlccht;
    private String cafMlcpt;
    private String cafMlces;

    public CAFMultiLanguageColumn() {
    }

    /**
     * 根据语言获取多语值
     *
     * @param langCode 语言编号
     */
    public String getValue(String langCode) {
        if (log.isDebugEnabled()) log.debug("Try to get multi-lang value with code: {} ", langCode);

        return getLanguageValue(langCode);
    }

    /**
     * 设置当前语言多语值
     *
     * @param value 多语值
     */
    public void setValue(String value) {
        String langCode = CAFCurrentLanguageResolver.getSessionInstance().getLanguage()
                .orElseGet(() -> {
                    if (CAFCurrentLanguageResolver.getRequestInstance() != null)
                        return CAFCurrentLanguageResolver.getRequestInstance().getLanguage().orElseThrow(NullPointerException::new);
                    else throw new NullPointerException();
                });

        if (log.isDebugEnabled()) log.debug("Current language is {}, value is {}", langCode, value);

        setLanguageValue(langCode, value);
    }

    /**
     * 设置多语值
     *
     * @param i18nValues 多语Map <语言编号,对应值>
     */
    @SuppressWarnings("unused")
    @JsonIgnore
    public void setValues(Map<String, String> i18nValues) {
        if (i18nValues != null) {
            i18nValues.forEach(this::setLanguageValue);
        }
    }

    /**
     * 返回多语Map <语言编号,对应值>
     */
    @SuppressWarnings("JpaAttributeTypeInspection")
    @JsonIgnore
    public Map<String, String> getValues() {
        Map<String, String> i18nValues = new HashMap<>();
        if (cafMlcchs != null) i18nValues.put("zh-CHS", cafMlcchs);
        if (cafMlcen != null) i18nValues.put("en", cafMlcen);
        if (cafMlccht != null) i18nValues.put("zh-CHT", cafMlccht);
        if (cafMlcpt != null) i18nValues.put("pt", cafMlcpt);
        if (cafMlces != null) i18nValues.put("es", cafMlces);

        return i18nValues;
    }

    /**
     * 根据当前语言打印信息
     */
    @Override
    public String toString() {
        String langCode = CAFCurrentLanguageResolver.getSessionInstance().getLanguage()
                .orElseGet(() -> {
                    if (CAFCurrentLanguageResolver.getRequestInstance() != null)
                        return CAFCurrentLanguageResolver.getRequestInstance().getLanguage().orElseThrow(NullPointerException::new);
                    else throw new NullPointerException();
                });

        if (log.isDebugEnabled()) log.debug("Current language is {}", langCode);

        return getLanguageValue(langCode);
    }


    private String getLanguageValue(String langCode) {
        if ("zh-CHS".equalsIgnoreCase(langCode)) {
            return cafMlcchs;
        } else if ("en".equalsIgnoreCase(langCode)) {
            return cafMlcen;
        } else if ("zh-CHT".equalsIgnoreCase(langCode)) {
            return cafMlccht;
        } else if ("pt".equalsIgnoreCase(langCode)) {
            return cafMlcpt;
        } else if ("es".equalsIgnoreCase(langCode)) {
            return cafMlces;
        } else {
            return cafMlcchs;
        }
    }


    private void setLanguageValue(String langCode, String value) {
        if ("zh-CHS".equalsIgnoreCase(langCode)) {
            this.cafMlcchs = value;
        } else if ("en".equalsIgnoreCase(langCode)) {
            this.cafMlcen = value;
        } else if ("zh-CHT".equalsIgnoreCase(langCode)) {
            this.cafMlccht = value;
        } else if ("pt".equalsIgnoreCase(langCode)) {
            this.cafMlcpt = value;
        } else if ("es".equalsIgnoreCase(langCode)) {
            this.cafMlces = value;
        }
    }

    @Override
    public final boolean equals(Object object) {
        if (this == object) return true;
        if (!(object instanceof CAFMultiLanguageColumn)) return false;

        CAFMultiLanguageColumn that = (CAFMultiLanguageColumn) object;
        return Objects.equals(getCafMlcchs(), that.getCafMlcchs()) && Objects.equals(getCafMlcen(), that.getCafMlcen()) && Objects.equals(getCafMlccht(), that.getCafMlccht()) && Objects.equals(getCafMlcpt(), that.getCafMlcpt()) && Objects.equals(getCafMlces(), that.getCafMlces());
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(getCafMlcchs());
        result = 31 * result + Objects.hashCode(getCafMlcen());
        result = 31 * result + Objects.hashCode(getCafMlccht());
        result = 31 * result + Objects.hashCode(getCafMlcpt());
        result = 31 * result + Objects.hashCode(getCafMlces());
        return result;
    }
}
